package cn.yzx.pethome.system.dept.service.impl;

import cn.yzx.pethome.base.service.impl.BaseServiceImpl;
import cn.yzx.pethome.system.dept.domain.Department;
import cn.yzx.pethome.system.dept.mapper.DepartmentMapper;
import cn.yzx.pethome.system.dept.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Transactional
    @Override
    public void add(Department department) {
        departmentMapper.add(department);
        buildDirPath(department);
        //修改当前的这条数据
        departmentMapper.update(department);
    }

    /**
     * 构建dirpath的路径
     * @param department
     */
    private void buildDirPath(Department department){
        if(department.getParent_id() == null){//没有父级
            //得到这个id
            Long id = department.getId();
            department.setDirPath("/"+id);
        }else {//有父级
            Long parentId = department.getParent_id();
            Department parent = departmentMapper.findOne(parentId);//父亲的所有数据
            String dirPath = parent.getDirPath();
            department.setDirPath(dirPath+"/"+department.getId());
        }
    }

    @Transactional
    @Override
    public void update(Department department) {
        if(department.getId().equals(department.getParent_id())){
            throw new RuntimeException("不能设置自己为自己的上级部门");
        }
        buildDirPath(department);
        departmentMapper.update(department);
    }

    @Transactional
    @Override
    public void delete(Long id) {
        //如果有下级 就不能删除
        Long count = departmentMapper.findByParentId(id);
        if(count == 0){
            departmentMapper.delete(id);
            //return;//结束当前方法
        }else {
            throw new RuntimeException("当前部门存在下级不能直接删除");
        }
    }

    @Override
    public List<Department> deptTree() {
        //第一步查询全部
        List<Department> departments = departmentMapper.findAll();

        //第三步 儿子找父亲 建立好关系网
        for (Department department : departments) {
            if(department.getParent_id() != null){//找顶级
                for (Department parent : departments) {
                    if(department.getParent_id() == parent.getId()){//判断是不是你的父亲
                        List<Department> children = parent.getChildren();
                        children.add(department);
                        break;
                    }
                }
            }
        }
        //第二步找顶级
        List<Department> firstTree = departments.stream()
                .filter(department -> department.getParent_id() == null)//过滤出来父id为null
                .collect(Collectors.toList());//转为list

        return firstTree;
    }
}